package com.zhn.evsi.task.Evsi;

import com.zhn.evsi.common.RedisCache;
import com.zhn.evsi.domain.dto.evsi.counter.EvsiCounterDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Slf4j
@Component
public class EvsiCounterTask {

    @Autowired
    private RedisCache redisCache;

    /**
     * 每日将当前计数器数据重置并赋值
     * */
    @Scheduled(cron = "0 1 0 * * ? ")
    public void counterDay() {
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        counterList.forEach(v -> {
            v.setDay(0);
            v.setWeekOld(v.getWeek());
            v.setMonthOld(v.getMonth());
            v.setYearOld(v.getYear());
        });
        redisCache.setCacheObject("Counter:" + username, counterList);
    }

    /**
     * 每周
     * */
    @Scheduled(cron = "0 3 0 ? * 1")
    public void counterWeek() {
        String username = "1085155177@qq.com";
        List<EvsiCounterDTO> counterList = redisCache.getCacheObject("Counter:" + username);
        counterList.forEach(v -> {
            v.setDay(0);
            v.setWeek(0);
            v.setWeekOld(0);
            v.setMonthOld(v.getMonth());
            v.setYearOld(v.getYear());
        });
        redisCache.setCacheObject("Counter:" + username, counterList);
    }
}
